<template>
  <el-dialog
    v-model="state.dialogVisible"
    :append-to-body="true"
    :close-on-click-modal="true"
    :close-on-press-escape="true"
    :destroy-on-close="true"
    top="5vh"
    :modal="model"
    :title="title"
    :width="width"
  >
    <el-row :gutter="0">
      <el-col :span="24">
        <el-form :inline="true" label-width="10px" :model="defTable.queryForm">
          <el-row :gutter="10">
            <el-col :span="6">
              <el-form-item label="">
                <el-input v-model="defTable.queryForm.tableName" placeholder="表名" />
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <el-form-item label="">
                <el-input v-model="defTable.queryForm.tableKey" placeholder="别名" />
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <el-form-item label="">
                <el-input v-model="defTable.queryForm.tableComment" placeholder="描述" />
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <el-form-item>
                <el-button icon="refresh-left" type="warning" @click="handleResetQuery">
                  重置
                </el-button>
                <el-button icon="search" type="primary" @click="queryTableData"> 查询 </el-button>
              </el-form-item>
            </el-col>
          </el-row>
        </el-form>
      </el-col>
    </el-row>
    <el-table
      ref="tableRef"
      v-loading="defTable.loading"
      border
      stripe
      :data="defTable.dataList"
      :height="defTable.height"
      :cell-style="tableStyle.cellStyle"
      :header-cell-style="tableStyle.headerCellStyle"
      @sort-change="handleSortChange"
      @selection-change="handleSelectionChange"
      @row-click="handleRowClick"
    >
      <el-table-column align="center" show-overflow-tooltip type="selection" width="50" />
      <el-table-column label="序号" type="index" width="55">
        <template #default="{ $index }">
          {{ $index + 1 }}
        </template>
      </el-table-column>
      <el-table-column
        align="center"
        label="中文名"
        prop="tableComment"
        show-overflow-tooltip
        width="180"
      />
      <el-table-column
        align="center"
        label="表名"
        prop="tableName"
        show-overflow-tooltip
        width="160"
      />
      <el-table-column
        align="center"
        label="别名"
        prop="tableKey"
        show-overflow-tooltip
        width="160"
      />
      <el-table-column align="center" label="类型" prop="tableModeDesc" width="120">
        <template #default="{ row }">
          <el-tag :type="row.tableMode === '0' ? 'success' : 'info'">
            {{ row.tableModeDesc }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column align="center" label="数据源" prop="dataSourceName" width="120">
        <template #default="{ row }">
          <el-tag :type="row.dataSourceCode === 'default' ? 'success' : 'info'">
            {{ row.dataSourceCode === "default" ? "默认数据源" : row.dataSourceCode }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column align="center" label="创建人" prop="createUserName" width="100" />
      <el-table-column
        align="center"
        :formatter="datetimeFormat"
        label="创建时间"
        prop="createTime"
        width="160"
      />
      <el-table-column align="center" label="最后修改人" prop="updateUserName" width="100" />
      <el-table-column
        align="center"
        :formatter="datetimeFormat"
        label="最后修改时间"
        prop="updateTime"
        width="160"
      />
    </el-table>
    <el-pagination
      background
      :current-page="defTable.pagination.current"
      :layout="defTable.pagination.layout"
      :page-size="defTable.pagination.size"
      :total="defTable.pagination.total"
      @current-change="handleCurrentChange"
      @size-change="handleSizeChange"
    />
    <template #footer>
      <el-button icon="circle-close" type="warning" @click="close"> 取消 </el-button>
      <el-button icon="circle-check" type="primary" @click="save"> 确定 </el-button>
    </template>
  </el-dialog>
</template>
<script setup lang="ts" name="DefTablePicker">
import { queryTablePage } from "@/api/def/table/";
import { cloneObject, datetimeFormat } from "@/utils/common";
import { useTable } from "@/hooks/table";
import type { BasicTableProps } from "@/hooks/table";

const props = defineProps({
  title: {
    type: String,
    default: "选择实体表"
  },
  width: {
    type: String,
    default: "60%"
  },
  model: {
    type: Boolean,
    default: true
  },
  ingore: {
    type: Array,
    default: function () {
      return [];
    }
  },
  multiple: {
    type: Boolean,
    default: function () {
      return false;
    }
  }
});

const emits = defineEmits(["change"]);

const tableRef = ref(null);

const state = reactive({
  dialogVisible: false
});

const initIgnore = () => {
  if (props.ingore.length > 0) {
    for (let i = 0; i < defTable.dataList.length; i++) {
      if (props.ingore.indexOf(defTable.dataList[i].id) >= 0) {
        defTable.pagination.total = defTable.pagination.total - 1;
        defTable.dataList.splice(i, 1);
        i = i - 1;
      }
    }
  }
};

const tableOptions = reactive<BasicTableProps>({
  autoLoad: false,
  multiple: props.multiple,
  queryForm: {
    tableKey: "",
    tableName: "",
    tableComment: "",
    dataSourceCode: "",
    tableTypeCode: "ALL"
  },
  queryFunction: queryTablePage,
  descs: ["create_time"],
  onLoaded: initIgnore
});

const {
  instance: defTable,
  tableStyle,
  queryTableData,
  handleSelectionChange,
  handleReset,
  handleResetQuery,
  handleRowClick,
  handleSizeChange,
  handleCurrentChange,
  handleSortChange,
  handleToggleHeight
} = useTable(tableOptions, tableRef);

const show = params => {
  cloneObject(defTable.queryForm, params);
  queryTableData();
  handleToggleHeight(80);
  state.dialogVisible = true;
};

const close = () => {
  handleReset();
  state.dialogVisible = false;
};

const save = () => {
  if (!props.multiple) {
    emits("change", defTable.dataSelections[0]);
  } else {
    emits("change", defTable.dataSelections);
  }
  close();
};

defineExpose({
  save,
  show,
  close
});
</script>
<style lang="scss" scoped>
.el-container {
  flex-direction: column;
}
</style>
